package com.fortune.rainshine.controller;

import com.fortune.rainshine.feign.common.RedisFeign;
import com.fortune.rainshine.feign.common.SmsSendFeign;
import com.fortune.rainshine.feign.system.CustomerFeign;
import com.fortune.rainshine.feign.system.CustomerPayCardFeign;
import com.fortune.rainshine.feign.system.UserFeign;
import com.fortune.rainshinecommon.base.MsgModel;
import com.fortune.rainshinecommon.enums.RedisConstantEnum;
import com.fortune.rainshinecommon.enums.ResultCode;
import com.fortune.rainshinecommon.enums.SmsTypeEnum;
import com.fortune.rainshinecommon.model.CustomerModel;
import com.fortune.rainshinecommon.model.SmsParamsModel;
import com.fortune.rainshinecommon.model.system.CustomerCardModel;
import com.fortune.rainshinecommon.model.system.CustomerPayCardModel;
import com.fortune.rainshinecommon.model.system.User;
import com.fortune.rainshinecommon.util.RandomNumUtil;
import com.google.gson.Gson;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @program: rainShine
 * @description:
 * @author: tangyn
 * @create: 2019-05-22
 **/
@RestController
@RequestMapping("customerPayCard")
public class CustomerPayCardController {
    private static final Logger logger = LoggerFactory.getLogger(CustomerPayCardController.class);

    @Autowired
    private CustomerFeign customerFeign;
    @Autowired
    private CustomerPayCardFeign customerPayCardFeign;
    @Autowired
    private RedisFeign redisFeign;
    @Autowired
    private SmsSendFeign smsSendFeign;
    @Value("${smsSend.number}")
    private Integer number;
    @Autowired
    private UserFeign userFeign;

    @RequiresPermissions("payCard:sms")
    @ResponseBody
    @RequestMapping("/sms")
    public MsgModel sms(@RequestBody CustomerCardModel customerCard) {
        try {
            CustomerModel customer = customerFeign.getCustomer(customerCard.getCustomerId());
            String phone = customer.getCustomerPhone();
            Boolean exists = redisFeign.exists(RedisConstantEnum.PAYCARD.getMessage() + phone);
            if (exists) {
                redisFeign.delete(RedisConstantEnum.PAYCARD.getMessage() + phone);
            }
            Gson gson = new Gson();
            String code = RandomNumUtil.getRandNum(number);
            SmsParamsModel smsParams = new SmsParamsModel();
            smsParams.setCode(code);
            smsParams.setSmsType(SmsTypeEnum.serviceValid.getKey());
            String jsonParams = gson.toJson(smsParams);

            Boolean bo = smsSendFeign.sendSms(phone, jsonParams);
            if (bo) {
                redisFeign.set(RedisConstantEnum.PAYCARD.getMessage() + phone, code, 60000);
            }
            return new MsgModel(ResultCode.SUCCESS);
        } catch (Exception e) {
            logger.error(e.toString());
            return new MsgModel(ResultCode.FAILURE);
        }
    }

    @RequiresPermissions("payCard:add")
    @ResponseBody
    @RequestMapping("/add")
    public MsgModel add(@RequestBody CustomerPayCardModel model) {
        try {
            //判断用户是否存在
            CustomerModel customer = customerFeign.getCustomer(model.getCustomerId());
            if (customer != null) {
                //redis key
                String key = RedisConstantEnum.PAYCARD.getMessage() + customer.getCustomerPhone();
                //判断是否存在于redis中
                Boolean exists = redisFeign.exists(key);
                if (exists) {
                    //获取redis中的keyc
                    String sms = (String) redisFeign.get(key).getData();
                    if (sms.equals(model.getVerificationCode())) {
//                        User user = userFeign.getUserById(model.getCreateId());
//                        model.setUser(user);
                        model.setUpdateId(model.getCreateId());
                        MsgModel msg = customerPayCardFeign.add(model);
                        redisFeign.delete(key);
                        return msg;
                    } else {
                        return new MsgModel("验证码不正确", ResultCode.FAILURE);
                    }
                } else {
                    return new MsgModel("验证码不存在或者已过期", ResultCode.FAILURE);
                }
            } else {
                return new MsgModel("用户不存在", ResultCode.FAILURE);
            }
        } catch (Exception e) {
            logger.error(e.toString());
            return new MsgModel(ResultCode.FAILURE);
        }
    }
}
